package org.mobile.beans;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.mobile.logic.OrderLogic;

public class Order {
	private int id;
	private Date date;
	private ArrayList<Phone> phones= new ArrayList<Phone>();
	private String status;
	public static final String STATUS_WAITING= "Waiting";
	public static final String STATUS_IN_PROGRESS= "In progress";
	public static final String STATUS_FINISHED= "Finished";
	private Order selectedOrder;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public ArrayList<Phone> getPhones() {
		return phones;
	}
	public void setPhones(ArrayList<Phone> phones) {
		this.phones = phones;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}	
		
	public Order getSelectedOrder() {
		return selectedOrder;
	}
	public void setSelectedOrder(Order selectedOrder) {
		this.selectedOrder = selectedOrder;
	}
	
	public String updateOrderStatus(){
		OrderLogic.getInstance().changeStatus(selectedOrder.status, selectedOrder.id);
		return "success";
	}
	
	public String makeOrder(){
		Map sessionMap= FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		User user= (User) sessionMap.get("user");
		user= user.getLoggedUser();
		List phones= (List) sessionMap.get("cart");
		boolean result= OrderLogic.getInstance().addOrder(user.getId(), phones, STATUS_WAITING);
		if(result){
			sessionMap.put("cart", null);
			List orders= OrderLogic.getInstance().getOrders(user.getId());
			user.setUserOrders((ArrayList<Order>) orders);
			return "success";
		}
		else{
			sessionMap.put("error", "Nie udalo sie zamowic");
			return "failure";
		}
	}

}
